package com.lambkit.module.cms.web.api.controller;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.shiro.authz.annotation.RequiresUser;

import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.lambkit.db.mgr.MgrTable;
import com.lambkit.db.sql.column.Example;
import com.lambkit.db.sql.condition.ConditionBuilder;
import com.lambkit.db.sql.condition.SqlBuilder;
import com.lambkit.module.cms.Cms;
import com.lambkit.module.cms.common.CmsConsts;
import com.lambkit.module.cms.common.CmsResult;
import com.lambkit.module.cms.common.CmsConsts.RefType;
import com.lambkit.module.cms.core.km.FileUtil;
import com.lambkit.module.cms.rpc.api.CmsFileService;
import com.lambkit.module.cms.rpc.model.CmsAccess;
import com.lambkit.module.cms.rpc.model.CmsArticle;
import com.lambkit.module.cms.rpc.model.CmsCategory;
import com.lambkit.module.cms.rpc.model.CmsFavorite;
import com.lambkit.module.cms.rpc.model.CmsFile;
import com.lambkit.module.cms.rpc.model.CmsTag;
import com.lambkit.module.upms.UpmsManager;
import com.lambkit.module.upms.rpc.model.UpmsUser;
import com.lambkit.web.controller.LambkitController;

@RequiresUser
public class KmApiController extends LambkitController {

	private static final Log LOG = Log.getLog(KmApiController.class);
	/**
	 * 获取某一file的信息
	 */
	public void index() {
		CmsFileService service = CmsFile.service();
		if(service==null) {
			renderJson(CmsResult.fail("service is null"));
			return;
		}
		Long id = getParaToLong(0, getParaToLong("id"));
		if (id!=null) {
			UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
    		if(user!=null && !user.getUserId().equals(1L)) {
    			CmsAccess.service().addFileAccess(id, user.getUserId(), "read");
    		}
			CmsFile model = service.findById(id);
			renderJson(CmsResult.success(model));
		} else {
			renderJson(CmsResult.fail("id is null"));
		}
	}

	/**
	 * 获取file列表
	 */
	public void list() {
		CmsFileService service = CmsFile.service();
		if(service==null) {
			renderJson(CmsResult.fail("service is null"));
			return;
		}
		MgrTable tbc = getTableView(service.getTableName(), true);
		if (tbc == null) {
			renderJson(CmsResult.fail("table is not find."));
			return;
		}
		SqlBuilder sb = new SqlBuilder();
		String select = sb.appendSelect(tbc).build();
		String sql = sb.clear().appendFrom(tbc).build();
		Integer pNumber = getParaToInt(1, 1);
		Integer pSize = getParaToInt(0, 15);
		pSize = pSize > CmsConsts.SQL_MAX_COUNT ? CmsConsts.SQL_MAX_COUNT : pSize;
		Page<Record> page = Db.paginate(pNumber, pSize, select, sql);
		renderJson(Ret.ok().set("data", page));
	}

	/**
	 * 获取file列表
	 */
	public void page() {
		CmsFileService service = CmsFile.service();
		if(service==null) {
			renderJson(CmsResult.fail("service is null"));
			return;
		}
		MgrTable tbc = getTableView(service.getTableName(), true);
		if (tbc == null) {
			renderJson(CmsResult.fail("table is not find."));
			return;
		}
		
		ConditionBuilder cb = getConditionsSQL(tbc).build("");
		SqlBuilder sb = new SqlBuilder();
		String select = sb.appendSelect(tbc).build();
		sb.clear().appendFrom(tbc).appendConditions(cb);
		String flags = getPara("flags");
		if(StrKit.notBlank(flags)) {
			StringBuilder strb = new StringBuilder();
			String[] flagsArry = flags.split(",");
			int i=0;
			for (String flag : flagsArry) {
				if(StrKit.notBlank(flag)) {
					if(i>0) strb.append(" and ");
					strb.append(" flag like '%").append(flag).append(",%'");
					i++;
				}
			}
			if(strb.length()>10) {
				sb.append(" and (").append(strb.toString()).append(") ");
			}
		}
		String sql = sb.build();
		Integer pNumber = getParaToInt(1, getParaToInt("pageNum", 1));
		Integer pSize = getParaToInt(0, getParaToInt("numPerPage", 15));
		pSize = pSize > CmsConsts.SQL_MAX_COUNT ? CmsConsts.SQL_MAX_COUNT : pSize;
		Page<Record> page = Db.paginate(pNumber, pSize, select, sql, cb.getSqlParas());
		renderJson(Ret.ok().set("data", page));
	}
	
	public void rename() {
		Long id = getParaToLong("id");
		if(id==null) {
			renderJson(CmsResult.fail("id is null"));
			return;
		}
		String title = getPara("title");
		if(StrKit.isBlank(title)) {
			renderJson(CmsResult.fail("title is null"));
			return;
		}
		CmsFile file = CmsFile.service().findById(id);
		file.setFileName(title);
		boolean flag = file.update();
		if(flag) {
			renderJson(CmsResult.success(flag));
		} else {
			renderJson(CmsResult.fail());
		}
	}
	
	/**
	 * 删除file，并移除目录关系
	 */
	public void delete() {
		String ids = getPara("ids");
		if(StrKit.isBlank(ids)) {
			renderJson(CmsResult.fail("ids is null"));
			return;
		}
		String[] idArray = ids.split(",");
		int n=0;
		for (String id : idArray) {
			Long fid = Long.parseLong(id);
			if(fid!=null) {
				try {
					n += CmsFile.service().delFileById(fid);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		if(n > 0) {
			renderJson(CmsResult.success(n));
		} else {
			renderJson(CmsResult.fail());
		}
	}
	
	/**
	 * 不删除file，仅移除目录关系
	 
	public void remove() {
		Long cid = getParaToLong("cid");
		String ids = getPara("ids");
		if(cid==null) {
			renderJson(CmsResult.fail("cid is null"));
			return;
		}
		if(StrKit.isBlank(ids)) {
			renderJson(CmsResult.fail("ids is null"));
			return;
		}
		int n = CmsFileCatalogMapping.service().removeFile(cid, ids);
		CmsFile.service().remove(ids);
		if(n > 0) {
			renderJson(Ret.ok().set("num", n));
		} else {
			renderJson(Ret.fail());
		}
	}*/
	
	public void move() {
		Long cid = getParaToLong("cid");
		Long pid = getParaToLong("pid");
		String ids = getPara("ids");
		if(cid==null) {
			renderJson(CmsResult.fail("cid is null"));
			return;
		}
		if(pid==null) {
			renderJson(CmsResult.fail("pid is null"));
			return;
		}
		if(StrKit.isBlank(ids)) {
			renderJson(CmsResult.fail("ids is null"));
			return;
		}
		int n = CmsFile.service().moveFile(cid, pid, ids);
		if(n > 0) {
			renderJson(Ret.ok().set("num", n));
		} else {
			renderJson(Ret.fail());
		}
	}

	
	/**
	 * 判断上传
	 */
	public void uploadSect() {
		DiskFileItemFactory factory = new DiskFileItemFactory();
    	ServletFileUpload sfu = new ServletFileUpload(factory);
    	sfu.setHeaderEncoding("utf-8");
    	
    	String savePath = Cms.config().getFilePath() + "uploadFile/";
    	
    	String fileMd5 = null;
    	String chunk = null;
    	
    	try {
			List<FileItem> items = sfu.parseRequest(getRequest());
			
			for(FileItem item:items){
				if(item.isFormField()){
					String fieldName = item.getFieldName();
					if(fieldName.equals("fileMd5")){
						fileMd5 = item.getString("utf-8");
					}
					if(fieldName.equals("chunk")){
						chunk = item.getString("utf-8");
					}
				}else{
					File file = new File(savePath+"/"+fileMd5);
					if(!file.exists()){
						file.mkdir();
					}
					File chunkFile = new File(savePath+"/"+fileMd5+"/"+chunk);
					FileUtils.copyInputStreamToFile(item.getInputStream(), chunkFile);
					
				}
			}
			
		} catch (FileUploadException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	renderJson(Ret.ok());
	}
	
	/**
	 * 上传结果
	 * @throws IOException
	 */
	public void upload() throws IOException {
        String savePath = Cms.config().getFilePath() + "uploadFile/";
        
		String action = getPara("action");
		
		if(action.equals("mergeChunks")){
			//合并文件
			//需要合并的文件的目录标记
			String fileMd5 = getPara("fileMd5");
			String fileName = getPara("fileName");
			String sufffix = fileName != null && fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".")) : null;
			//String path = "/uploadFile/" + fileMd5 + sufffix;
			
			Long catalogId = getParaToLong("catalogId", 1L);
					
			//读取目录里的所有文件
			File f = new File(savePath+"/"+fileMd5);
			File[] fileArray = f.listFiles(new FileFilter(){
				//排除目录只要文件
				@Override
				public boolean accept(File pathname) {
					// TODO Auto-generated method stub
					if(pathname.isDirectory()){
						return false;
					}
					return true;
				}
			});
			
			//转成集合，便于排序
			List<File> fileList = new ArrayList<File>(Arrays.asList(fileArray));
			Collections.sort(fileList,new Comparator<File>() {
				@Override
				public int compare(File o1, File o2) {
					// TODO Auto-generated method stub
					if(Integer.parseInt(o1.getName()) < Integer.parseInt(o2.getName())){
						return -1;
					}
					return 1;
				}
			});
			//UUID.randomUUID().toString()-->随机名
			File outputFile = new File(savePath+"/"+fileMd5+".mp4");
			//创建文件
			outputFile.createNewFile();
			//输出流
			FileChannel outChnnel = new FileOutputStream(outputFile).getChannel();
			//合并
			FileChannel inChannel;
			for(File file : fileList){
				inChannel = new FileInputStream(file).getChannel();
				inChannel.transferTo(0, inChannel.size(), outChnnel);
				inChannel.close();
				//删除分片
				file.delete();
			}
			outChnnel.close();
			//清除文件夹
			File tempFile = new File(savePath+"/"+fileMd5);
			if(tempFile.isDirectory() && tempFile.exists()){
				tempFile.delete();
			}
			System.out.println("合并成功");
			
			Long fileSize = outputFile.length();
			
			UpmsUser user = UpmsManager.me().currentUser();
			String userFilePath = "common/files";
			if(user!=null) {
				userFilePath = "users/" + user.getUserId() + "/files";
			}
			
			String nFilePath = getPara("filePath", userFilePath);
			//移动到指定位置
			String newPath = FileUtil.moveCmsFile(outputFile, nFilePath, fileMd5, sufffix);
			List<String> uploadfiles = (List<String>) getSession().getAttribute(CmsConsts.SESSION_UPLOADS);
			if (uploadfiles == null) {
				uploadfiles = new ArrayList<>();
				getSession().setAttribute(CmsConsts.SESSION_UPLOADS, uploadfiles);
			}
			//加入session
			uploadfiles.add(fileMd5);
			//存入数据库
			CmsFile cmsFile = new CmsFile();
			cmsFile.setFileCreateTime(new Date());
			cmsFile.setFileName(FileUtil.getFileName(fileName).toString());
			cmsFile.setFilePath(newPath);
			cmsFile.setFileExt(sufffix.substring(1));
			cmsFile.setFileUuid(fileMd5);
			cmsFile.setFileUserId(user.getUserId());
			cmsFile.setFileState(2);
			cmsFile.setFileCategoryId(catalogId);
			cmsFile.setFileSize(fileSize);
			CmsFile.service().insertFile(cmsFile);
			// 处理文件
			new Thread() {
				public void run() {
					try {
						CmsFile.service().translateFile(fileMd5);
					} catch (IOException e) {
						LOG.error(fileMd5 + "文件处理失败");
					}
				};
			}.start();
			renderJson(CmsResult.success(cmsFile));
		} else if(action.equals("checkChunk")){
			//检查当前分块是否上传成功
			String fileMd5 = getPara("fileMd5");
			String chunk = getPara("chunk");
			String chunkSize = getPara("chunkSize");
			
			File checkFile = new File(savePath+"/"+fileMd5+"/"+chunk);
			
			//检查文件是否存在，且大小是否一致
			if(checkFile.exists() && checkFile.length()==Integer.parseInt(chunkSize)){
				//上传过
				renderJson(Ret.ok());
			}else{
				//没有上传过
				//response.getWriter().write("{\"ifExist\":0}");
				renderJson(Ret.fail());
			}
		}
	}
	
	/**
	 * 目录相关服务
	 */
	public void catalog() {
		String action = getPara(0, "parent");
		if("link".equalsIgnoreCase(action)) {
			//分类地址链
			Long parentId = getParaToLong(1, 1L);
			renderJson(CmsResult.success(CmsCategory.service().linkParentById(parentId)));
		} else if("tree".equalsIgnoreCase(action)) {
			//某一用户的所有分类，树结构，layui使用
			Long userId = getParaToLong(1, 1L);
			Long systemId = getParaToLong(2, 5L);
			UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
			userId = user!=null && user.getUserId().equals(userId) ? userId : 1L;
			renderJson(CmsResult.success(CmsCategory.service().menuTreeBySystemId(systemId, userId, 0)));
		} else if("menu".equalsIgnoreCase(action)) {
			//某一用户的所有分类，树结构，layui使用
			Long userId = getParaToLong(1, 1L);
			Long categoryId = getParaToLong(2, 0L);
			UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
			userId = user!=null && user.getUserId().equals(userId) ? userId : 1L;
			renderJson(CmsResult.success(CmsCategory.service().menuTreeByParentid(categoryId, userId, 0)));
		} else if("sort".equalsIgnoreCase(action)) {
			//某一用户的所有分类，树结构，SortModel使用
			Long userId = getParaToLong(1, 1L);
			Long systemId = getParaToLong(2, 5L);
			UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
			userId = user!=null && user.getUserId().equals(userId) ? userId : 1L;
			renderJson(CmsResult.success(CmsCategory.service().treeBySystemId(systemId, userId)));
		} else if("files".equalsIgnoreCase(action)) {
			//某一文件夹下的所有文件
			Long categoryId = getParaToLong(1, 1L);
			int num = getParaToInt(2, 1);
			int row = getParaToInt(3, 10);
			row = row > CmsConsts.SQL_MAX_COUNT ? CmsConsts.SQL_MAX_COUNT : row;
			String orders = getPara(4, "file_create_time desc");
			Example example = CmsFile.sql().andFileCategoryIdEqualTo(categoryId).example().setOrderBy(orders);
			Page<CmsFile> files = CmsFile.service().paginate(num, row, example);
			renderJson(CmsResult.success(files));
		} else if("folder".equalsIgnoreCase(action)) {
			//用户库的某一文件夹下的文件夹列表
			Long parentId = getParaToLong(1, 1L);
			UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
			renderJson(CmsResult.success(CmsCategory.service().findByParenId(parentId, user.getUserId())));
		} else if("pubf".equalsIgnoreCase(action)) {
			//公共库的某一文件夹下的文件夹列表
			Long parentId = getParaToLong(1, 1L);
			renderJson(CmsResult.success(CmsCategory.service().findByParenId(parentId, 1L)));
		} else if("fn".equalsIgnoreCase(action)) {
			//公共库的某一文件夹下的文件夹列表
			Long cid1 = getParaToLong(1);
			Long cid2 = getParaToLong(2);
			int num = getParaToInt(3, 1);
			int row = getParaToInt(4, 10);
			Long uid = getParaToLong(5, 1L);
			if(!uid.equals(1L)) {
				UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
				uid = user.getUserId().equals(uid) ? uid : 1L;
			}
			renderJson(CmsResult.success(CmsCategory.service().getFileAndNote(num, row, cid1, cid2, uid)));
		} else {
			//网盘，所有文件和文件夹
			Long parentId = getParaToLong(1, 1L);
			Long systemId = getParaToLong(2, 5L);
			UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
			List<Record> categorys = CmsCategory.service().getAll(parentId, user.getUserId(), systemId);
	    	renderJson(CmsResult.success(categorys));
		}
	}
	
	/**
	 * 文件
	 */
	public void file() {
		String method=getPara("m");
		Long categoryId = getParaToLong("cid", 1L);
		int num = getParaToInt("page", 1);
		int row = getParaToInt("size", 10);
		String title = getPara("title");
		String tags = getPara("tags");
		if("all".equalsIgnoreCase(method)) {
			renderJson(CmsResult.success(CmsFile.service().getPublishByCategory(num, row, categoryId, title, tags)));
		} else {
			renderJson(CmsResult.success(CmsFile.service().getPublishOnCategory(num, row, categoryId, title, tags)));
		}
    }

	/**
	 * 笔记
	 */
	public void note() {
		String action = getPara(0);
		if("my".equalsIgnoreCase(action)) {
			//我的近期笔记和文件
			int num = getParaToInt(1, 1);
			int row = getParaToInt(2, 10);
			row = row > CmsConsts.SQL_MAX_COUNT ? CmsConsts.SQL_MAX_COUNT : row;
			UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
			renderJson(CmsResult.success(CmsFile.service().getRecentFileAndNote(num, row, user.getUserId())));
		} else if("pub".equalsIgnoreCase(action)) {
			//公共库的近期笔记和文件
			int num = getParaToInt(1, 1);
			int row = getParaToInt(2, 10);
			renderJson(CmsResult.success(CmsFile.service().getRecentFileAndNote(num, row, 1L)));
		} else if("fn".equalsIgnoreCase(action)) {
			//公共库的近期笔记和文件
			Long categoryId = getParaToLong(1, 1L); 
			int num = getParaToInt(2, 1);
			int row = getParaToInt(3, 10);
			renderJson(CmsResult.success(CmsFile.service().getPublishOnCategory(num, row, categoryId, null, null)));
		} else {
			//我的笔记
			Long noteId = getParaToLong(0);
			UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
			if(user!=null && !user.getUserId().equals(1L)) {
				CmsAccess.service().addArticleAccess(noteId, user.getUserId(), "read");
			}
			renderJson(CmsResult.success(CmsArticle.service().findById(noteId)));
		}
	}
	
	public void tag() {
		String action = getPara(0);
		if("list".equalsIgnoreCase(action)) {
			Long userId = getParaToLong(1, 1L);
			Long systemId = getParaToLong(2, 5L);
			UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
			userId = user!=null && user.getUserId().equals(userId) ? userId : 1L;
			renderJson(CmsResult.success(CmsTag.service().findTagBySystemAndUserId(systemId, userId)));
		} else if("note".equalsIgnoreCase(action)) {
			String tag = getPara(1);
			int num = getParaToInt(2, 1);
			int row = getParaToInt(3, 10);
			row = row > CmsConsts.SQL_MAX_COUNT ? CmsConsts.SQL_MAX_COUNT : row;
			Long userId = getParaToLong(4, 1L);
			UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
			userId = user!=null && user.getUserId().equals(userId) ? userId : 1L;
			renderJson(CmsResult.success(CmsArticle.service().paginateByUserTag(num, row, userId, tag)));
		} else if("file".equalsIgnoreCase(action)) {
			String tag = getPara(1);
			int num = getParaToInt(2, 1);
			int row = getParaToInt(3, 10);
			row = row > CmsConsts.SQL_MAX_COUNT ? CmsConsts.SQL_MAX_COUNT : row;
			Long userId = getParaToLong(4, 1L);
			UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
			userId = user!=null && user.getUserId().equals(userId) ? userId : 1L;
			renderJson(CmsResult.success(CmsFile.service().paginateByUserTag(num, row, userId, tag)));
		}
	}
	
	/**
	 * 收藏
	 */
	public void fork() {
    	String method = getPara("m");
    	if("judge".equalsIgnoreCase(method)) {
    		//判断是否收藏过
    		UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
    		Long refid = getParaToLong("id");
    		if(user==null || refid==null) {
    			renderJson(CmsResult.fail());
    			return;
    		}
    		boolean f = CmsFavorite.service().judge(user.getUserId(), refid, RefType.File);
    		if(f) {
    			renderJson(CmsResult.success(refid));
    		} else {
    			renderJson(CmsResult.fail());
    		}
    	} else if("delete".equalsIgnoreCase(method)) {
    		//删除一个收藏
    		UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
    		Long refid = getParaToLong("id");
    		if(user==null || refid==null) {
    			renderJson(CmsResult.fail());
    			return;
    		}
    		boolean f = CmsFavorite.service().unfork(user.getUserId(), refid, RefType.File);
    		if(f) {
    			renderJson(CmsResult.success(refid));
    		} else {
    			renderJson(CmsResult.fail());
    		}
    	} else if("insert".equalsIgnoreCase(method)) {
    		//增加一个收藏
    		UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
    		Long refid = getParaToLong("id");
    		if(user==null || refid==null) {
    			renderJson(CmsResult.fail());
    			return;
    		}
    		Long cid = getParaToLong("cid", 0L);
    		String note = getPara("note");
    		boolean f = CmsFavorite.service().forkNote(user.getUserId(), refid, RefType.File, cid, note);
    		if(f) {
    			renderJson(CmsResult.success(refid));
    		} else {
    			renderJson(CmsResult.fail());
    		}
    	} else if("add".equalsIgnoreCase(method)) {
    		//增加一个收藏
    		UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
    		Long refid = getParaToLong("id");
    		if(user==null || refid==null) {
    			renderJson(CmsResult.fail());
    			return;
    		}
    		Long cid = getParaToLong("cid", 0L);
    		String tags = getPara("tags");
    		boolean f = CmsFavorite.service().fork(user.getUserId(), refid, RefType.File, cid, tags);
    		if(f) {
    			renderJson(CmsResult.success(refid));
    		} else {
    			renderJson(CmsResult.fail());
    		}
    	} else {
    		//获取收藏
    		UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
    		Long refid = getParaToLong("id");
    		if(user==null || refid==null) {
    			renderJson(CmsResult.fail());
    			return;
    		}
    		CmsFavorite f = CmsFavorite.service().get(user.getUserId(), refid, RefType.File);
    		if(f!=null) {
    			renderJson(CmsResult.success(f));
    		} else {
    			renderJson(CmsResult.fail());
    		}
        }
    }
}
